package com.operation.service.impl;

import com.operation.beans.LevelDirectory;
import com.operation.beans.Result;
import com.operation.dao.OneLevelDirectoryMapper;
import com.operation.dao.TwoLevelDirectoryMapper;
import com.operation.dao.UserInfoMapper;
import com.operation.entity.OneLevelDirectory;
import com.operation.entity.TwoLevelDirectory;
import com.operation.entity.UserInfo;
import com.operation.service.LevelDirectoryService;
import com.operation.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LevelDirectoryServiceImpl implements LevelDirectoryService {
    @Autowired
    OneLevelDirectoryMapper oneLevelDirectoryMapper;
    @Autowired
    TwoLevelDirectoryMapper twoLevelDirectoryMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    OneLevelDirectory one = new OneLevelDirectory();
    TwoLevelDirectory two = new TwoLevelDirectory();
    int temp = 0;

    /**
     * 添加一条目录
     * @param levelDirectory
     * @return
     */
    @Override
    public Result addOneRecord(LevelDirectory levelDirectory) {

            if ((oneLevelDirectoryMapper.oneIsExit(levelDirectory.getOne()))==null) {
                one.setOne(levelDirectory.getOne());
                one.setoType(oneLevelDirectoryMapper.selectAllOneLevel().size() + 1);
                oneLevelDirectoryMapper.insert(one);
                temp = oneLevelDirectoryMapper.selectAllOneLevel().size() + 1;
                UserInfo userInfo = new UserInfo();
                userInfo.setUserName(levelDirectory.getUserName());
                userInfo.setUserType(oneLevelDirectoryMapper.selectAllOneLevel().size());
                userInfoMapper.updateTypeByUserName(userInfo);
            }else{
                //-1表示用户已存在
                temp = -1;
            }

        return ResultUtil.success(temp,0);
    }

    @Override
    public Result deleteOneRecord(LevelDirectory levelDirectory) {
        if (levelDirectory.getOne() == null){
            temp = twoLevelDirectoryMapper.deleteByTwo(levelDirectory.getTwo());
        }else {
            temp = oneLevelDirectoryMapper.deleteByOne(levelDirectory.getOne());
        }
        return ResultUtil.success(temp,0);
    }

    @Override
    public Result updateOneRecord(LevelDirectory levelDirectory) {

        temp = oneLevelDirectoryMapper.updateByOneSelective(levelDirectory.getOne(),levelDirectory.getUpdateOne());

        return ResultUtil.success(temp,0);
    }

    @Override
    public Result queryAllTwoLevelRecord(Integer tType) {
        return ResultUtil.success(twoLevelDirectoryMapper.selectByTType(tType),0);
    }

    @Override
    public Result queryAllOneLevelRecord() {
        return ResultUtil.success(oneLevelDirectoryMapper.selectAllOneLevel(),0);
    }

    @Override
    public OneLevelDirectory selectOType(String one) {
        return oneLevelDirectoryMapper.selectOType(one);
    }

    @Override
    public OneLevelDirectory selectOne(int oType) {
        return oneLevelDirectoryMapper.selectOne(oType);
    }
}
